﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using uz.control.CustomControl;

namespace uz.control
{
    public class TNB_Label : UzTreeNode
    {
        public Control Editor { get; set; }

        public override void DrawNodeText(PaintEventArgs e, Brush brush, Rectangle Bounds)
        {
            SizeF sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, Text);
            float xOffset = HandleAreaWidth + GetIndentWidth();
            float yOffset = 5;
            e.Graphics.DrawString(Text, UzTreeView.Font, brush, Bounds.Left + xOffset , Bounds.Top + yOffset);
        }
        
        public SizeF GetDrawTextSizeF(Graphics g,Font font, string Text)
        {
            return g.MeasureString(Text, font);
        }

        public override void DrawNodeContent(PaintEventArgs e, Brush BackBrush, Brush ForeBrush, Brush SelectedBackBrush, Brush SelectedForeBrush, Rectangle Bounds)
        {
            if (IsEdit == false)
            {   
                base.DrawNodeContent(e, BackBrush, ForeBrush, SelectedBackBrush, SelectedForeBrush, Bounds);
            }
            else
            {
                e.Graphics.FillRectangle(
                    Brushes.White, 
                    Bounds.Left + GetIndentWidth(),
                    Bounds.Top,
                    Bounds.Width,
                    Bounds.Height
                );
                UzTextBox txt = UzTreeView.Controls["txtEditor"] as CustomControl.UzTextBox;
                float xOffset = HandleAreaWidth + GetIndentWidth();
                if (txt == null)
                {
                    txt = Editor as CustomControl.UzTextBox;
                    txt.Top = Bounds.Bottom - txt.Height;
                    txt.Left = Bounds.Left + (int)xOffset + 1;
                    txt.Width = Bounds.Width - (int)xOffset + 1;
                    UzTreeView.Controls.Add(txt);
                    txt.Focus();
                    BeginEdit(txt);
                }

                txt.Top = Bounds.Bottom - txt.Height;
                txt.Left = Bounds.Left + (int)xOffset + 1;
                txt.Width = Bounds.Width - (int)xOffset + 1;
                
            }
        }

        public virtual string Text { get; set; }

        public override bool IsEdit
        {
            get
            {
                return base.IsEdit;
            }
            set
            {
                base.IsEdit = value;

                if (value == true)
                {
                    if (UzTreeView.CurrentEditedNode == this)
                    {
                        return;
                    }

                    UzTreeView.SelectedNode = this;
                    if (UzTreeView.CurrentEditedNode != null)
                    {
                        UzTreeView.CurrentEditedNode.IsEdit = false;
                    }

                    UzTreeView.CurrentEditedNode = this;

                    CustomControl.UzTextBox txt = new CustomControl.UzTextBox(this);
                    txt.BorderStyle = BorderStyle.None;
                    txt.Name = "txtEditor";
                    txt.KeyDown += new KeyEventHandler(txt_KeyDown);
                    Editor = txt;
                }
                else
                {
                    UzTreeView.Controls.Clear();
                    UzTreeView.CurrentEditedNode = null;
                    Editor = null;
                }
            }
        }

        void txt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (EndEdit(sender as UzTextBox) == true)
                {
                    IsEdit = false;
                }
                UzTreeView.Refresh();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                IsEdit = false;
            }
        }

        public virtual void BeginEdit(UzTextBox txt)
        {
            
        }

        public virtual bool EndEdit(UzTextBox txt)
        {
            return true;
        }
    }
}
